package com.vrv.controller.log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vrv.entity.AdminInfo;
import com.vrv.entity.Form;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;
import com.vrv.utils.XMLRead;

@Controller
@RequestMapping("/syslog")
public class SysLogController {

    @Autowired
    HttpServletRequest request;

    @RequestMapping("/list")
    public String list() {
        return "/syslog/list";
    }

    @RequestMapping(value = "getLogList", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getLogList(Form form) {
        HttpSession session = request.getSession();
        Integer userType = 0;
        if (session.getAttribute("type") != null) {
            if (Integer.parseInt(session.getAttribute("type").toString()) == 1) {
                AdminInfo userInfo = (AdminInfo) session.getAttribute("userInfo");
                userType = userInfo.getType();
            }
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "select_table");
        params.put("service", "all");
        params.put("object", "table");
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell = new HashMap<String, Object>();
        paramsCell.put("name", "loginfo left join userinfo on userinfo.account=loginfo.account");
        paramsCell.put("asname", "");
        mapList.add(paramsCell);
        Map<String, Object> tableInfoMap = new HashMap<String, Object>();
        tableInfoMap.put("tables", mapList);

        Integer currentPage = form.getCurrentPage() == null ? 1 : form.getCurrentPage();
        Integer pageCount = form.getPageCount() == null ? 10 : form.getPageCount();
        tableInfoMap.put("from", (currentPage - 1) * 10);
        tableInfoMap.put("count", pageCount);

        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell2 = new HashMap<String, Object>();
        paramsCell2.put("name", "loginfo.*");
        paramsCell2.put("asname", "");
        mapList2.add(paramsCell2);

        tableInfoMap.put("cols", mapList2);

        List<Map<String, Object>> mapList3 = new ArrayList<Map<String, Object>>();
        if (form.getSysRole() != null && !"".equals(form.getSysRole())) {
            Map<String, Object> paramsObj3 = new HashMap<String, Object>();
            paramsObj3.put("userinfo.type", form.getSysRole());
            Map<String, Object> paramsObj4 = new HashMap<String, Object>();
            paramsObj4.put("term", paramsObj3);
            mapList3.add(paramsObj4);
        }

        if (form.getType() != null && !"".equals(form.getType())) {
            Map<String, Object> paramsObj3 = new HashMap<String, Object>();
            paramsObj3.put("loginfo.module", form.getType());
            Map<String, Object> paramsObj4 = new HashMap<String, Object>();
            paramsObj4.put("term", paramsObj3);
            mapList3.add(paramsObj4);
        }

        if (form.getContent() != null && !"".equals(form.getContent())) {
            Map<String, Object> paramsObj7 = new HashMap<String, Object>();
            paramsObj7.put("loginfo.content", "%%" + form.getContent() + "%%");
            Map<String, Object> paramsObj8 = new HashMap<String, Object>();
            paramsObj8.put("like", paramsObj7);
            mapList3.add(paramsObj8);
        }

        if (form.getBeginDate() != null && !"".equals(form.getBeginDate())
                && form.getEndDate() != null && !"".equals(form.getEndDate())) {
            Map<String, Object> paramsObj7 = new HashMap<String, Object>();
            paramsObj7.put("gte", form.getBeginDate());
            paramsObj7.put("lte", form.getEndDate());
            Map<String, Object> paramsObj8 = new HashMap<String, Object>();
            paramsObj8.put("loginfo.addtime", paramsObj7);
            Map<String, Object> paramsObj9 = new HashMap<String, Object>();
            paramsObj9.put("range", paramsObj8);
            mapList3.add(paramsObj9);
        }

        if (userType == 3 || userType == 1) {

        } else if (userType == 2) {

        } else {

        }

        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("must", mapList3);
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("bool", paramsObj5);
        if ((form.getSysRole() != null && !"".equals(form.getSysRole()))
                || (form.getType() != null && !"".equals(form.getType()))
                || (form.getBeginDate() != null && !"".equals(form.getBeginDate())
                        && form.getEndDate() != null && !"".equals(form.getEndDate()))
                || (form.getContent() != null && !"".equals(form.getContent()))) {
            tableInfoMap.put("where", paramsObj6);
        }

        params.put("tableinfo", tableInfoMap);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "getLogListCount", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getLogListCount(Form form) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "select_table");
        params.put("service", "all");
        params.put("object", "table");
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell = new HashMap<String, Object>();
        paramsCell.put("name", "loginfo left join admininfo on admininfo.account=loginfo.account");
        paramsCell.put("asname", "");
        mapList.add(paramsCell);
        Map<String, Object> tableInfoMap = new HashMap<String, Object>();
        tableInfoMap.put("tables", mapList);
        Integer currentPage = form.getCurrentPage() == null ? 1 : form.getCurrentPage();
        Integer pageCount = form.getPageCount() == null ? 10 : form.getPageCount();
        tableInfoMap.put("from", (currentPage - 1) * 10);
        tableInfoMap.put("count", pageCount);

        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell2 = new HashMap<String, Object>();
        paramsCell2.put("name", "count(*)");
        paramsCell2.put("asname", "count");
        mapList2.add(paramsCell2);
        tableInfoMap.put("cols", mapList2);
        List<Map<String, Object>> mapList3 = new ArrayList<Map<String, Object>>();
        if (form.getSysRole() != null && !"".equals(form.getSysRole())) {
            Map<String, Object> paramsObj3 = new HashMap<String, Object>();
            paramsObj3.put("userinfo.type", form.getSysRole());
            Map<String, Object> paramsObj4 = new HashMap<String, Object>();
            paramsObj4.put("term", paramsObj3);
            mapList3.add(paramsObj4);
        }

        if (form.getType() != null && !"".equals(form.getType())) {
            Map<String, Object> paramsObj3 = new HashMap<String, Object>();
            paramsObj3.put("loginfo.module", form.getType());
            Map<String, Object> paramsObj4 = new HashMap<String, Object>();
            paramsObj4.put("term", paramsObj3);
            mapList3.add(paramsObj4);
        }

        if (form.getContent() != null && !"".equals(form.getContent())) {
            Map<String, Object> paramsObj7 = new HashMap<String, Object>();
            paramsObj7.put("loginfo.content", "%%" + form.getContent() + "%%");
            Map<String, Object> paramsObj8 = new HashMap<String, Object>();
            paramsObj8.put("like", paramsObj7);
            mapList3.add(paramsObj8);
        }

        if (form.getBeginDate() != null && !"".equals(form.getBeginDate())
                && form.getEndDate() != null && !"".equals(form.getEndDate())) {
            Map<String, Object> paramsObj7 = new HashMap<String, Object>();
            paramsObj7.put("gte", form.getBeginDate());
            paramsObj7.put("lte", form.getEndDate());
            Map<String, Object> paramsObj8 = new HashMap<String, Object>();
            paramsObj8.put("loginfo.addtime", paramsObj7);
            Map<String, Object> paramsObj9 = new HashMap<String, Object>();
            paramsObj9.put("range", paramsObj8);
            mapList3.add(paramsObj9);
        }
        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("must", mapList3);
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("bool", paramsObj5);
        if ((form.getSysRole() != null && !"".equals(form.getSysRole()))
                || (form.getType() != null && !"".equals(form.getType()))
                || (form.getBeginDate() != null && !"".equals(form.getBeginDate())
                        && form.getEndDate() != null && !"".equals(form.getEndDate()))
                || (form.getContent() != null && !"".equals(form.getContent()))) {
            tableInfoMap.put("where", paramsObj6);
        }
        params.put("tableinfo", tableInfoMap);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

}
